﻿using System;
namespace ZYCommon
{
    

    /// <summary>
    /// 字符缓冲  没有这个类也可以运行
    /// </summary>
    public class ByteBuffer
    {
        private byte[] bytBytes = new byte[0x10];
        private int intCount = 0;

        public void Append(byte bytData)
        {
            lock (this)
            {
                if (this.intCount >= this.bytBytes.Length)
                {
                    byte[] buffer = new byte[(int) (this.intCount * 1.5)];
                    for (int i = 0; i < this.bytBytes.Length; i++)
                    {
                        buffer[i] = this.bytBytes[i];
                    }
                    this.bytBytes = buffer;
                }
                this.bytBytes[this.intCount] = bytData;
                this.intCount++;
            }
        }

        public void Clear()
        {
            lock (this)
            {
                this.bytBytes = new byte[0x10];
                this.intCount = 0;
            }
        }

        public byte[] ToByteArray()
        {
            byte[] buffer = null;
            lock (this)
            {
                buffer = new byte[this.intCount];
                for (int i = 0; i < this.intCount; i++)
                {
                    buffer[i] = this.bytBytes[i];
                }
            }
            return buffer;
        }

        public int Count
        {
            get
            {
                return this.intCount;
            }
        }

        public byte FirstByte
        {
            get
            {
                if (this.intCount > 0)
                {
                    return this.bytBytes[0];
                }
                return 0;
            }
        }

        public byte LastByte
        {
            get
            {
                if (this.intCount > 0)
                {
                    return this.bytBytes[this.intCount - 1];
                }
                return 0;
            }
        }
    }
}

